﻿using System;
using System.Configuration;
using System.Web.Caching;
using System.Collections.Generic;

namespace Hxsoft.AppV1.Module
{
	public class WebGenericCacheContainer : IGenericCacheContainer
	{
		private Cache cache = System.Web.HttpContext.Current.Cache;
		protected Cache Cache
		{
			get { return cache; }
		}

		private static readonly string CacheDatabaseName = StringExtensions.GetEffectiveValueOrDefault(ConfigurationManager.AppSettings["CacheDatabaseName"], "FC308");



		protected static readonly char[] DependencyTableSeparator = new char[] { ',' };
		protected static string[] GetDependencyTables(string configKey, string defaultValue)
		{
			string val = StringExtensions.GetEffectiveValueOrDefault(ConfigurationManager.AppSettings[configKey], defaultValue);
			if (string.IsNullOrEmpty(val)) return null;
			return val.Split(DependencyTableSeparator);
		}

		protected CacheDependency GetTableCacheDependency(string[] tables)
		{
			if (tables == null) return null;

			if (tables.Length == 1)
				return new System.Web.Caching.SqlCacheDependency(CacheDatabaseName, tables[0]);

			AggregateCacheDependency dependency = new AggregateCacheDependency();
			foreach (string tableName in tables)
				dependency.Add(new System.Web.Caching.SqlCacheDependency(CacheDatabaseName, tableName));

			return dependency;
		}



		#region Cache 成员
		private static readonly string[] UserIdByUsernameDictionaryDependencyTables = GetDependencyTables("UserIdByUsernameDictionaryTableDependency", null);
		private const string UserIdByUsernameDictionaryKeyName = "UserIdByUsernameDictionary";

		public Dictionary<string, Guid?> UserIdByUsernameDictionary
		{
			get
			{
				Dictionary<string, Guid?> c = cache[UserIdByUsernameDictionaryKeyName] as Dictionary<string, Guid?>;
				if (c != null) return c;

				c = new Dictionary<string, Guid?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(UserIdByUsernameDictionaryKeyName, c, GetTableCacheDependency(UserIdByUsernameDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] AdministratorDictionaryDependencyTables = GetDependencyTables("AdministratorDictionaryTableDependency", "UsersInRoles");
		private const string AdministratorDictionaryKeyName = "AdministratorDictionary";

		public Dictionary<string, bool> AdministratorDictionary
		{
			get
			{
				Dictionary<string, bool> c = cache[AdministratorDictionaryKeyName] as Dictionary<string, bool>;
				if (c != null) return c;

				c = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(AdministratorDictionaryKeyName, c, GetTableCacheDependency(AdministratorDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] CurrentSiteDictionaryDependencyTables = GetDependencyTables("CurrentSiteDictionaryTableDependency", "Site");
		private const string CurrentSiteDictionaryKeyName = "CurrentSiteDictionary";

		public Dictionary<string, Site> CurrentSiteDictionary
		{
			get
			{
				Dictionary<string, Site> c = cache[CurrentSiteDictionaryKeyName] as Dictionary<string, Site>;
				if (c != null) return c;

				c = new Dictionary<string, Site>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(CurrentSiteDictionaryKeyName, c, GetTableCacheDependency(CurrentSiteDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] SiteDictionaryDependencyTables = GetDependencyTables("SiteDictionaryTableDependency", "Site");
		private const string SiteDictionaryKeyName = "SiteDictionary";

		public Dictionary<int, Site> SiteDictionary
		{
			get
			{
				Dictionary<int, Site> c = cache[SiteDictionaryKeyName] as Dictionary<int, Site>;
				if (c != null) return c;

				c = new Dictionary<int, Site>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(SiteDictionaryKeyName, c, GetTableCacheDependency(SiteDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] DicNameByIdDictionaryDependencyTables = GetDependencyTables("DicNameByIdDictionaryTableDependency", "Dictionary");
		private const string DicNameByIdDictionaryKeyName = "DicNameByIdDictionary";

		public Dictionary<string, string> DicNameByIdDictionary
		{
			get
			{
				Dictionary<string, string> c = cache[DicNameByIdDictionaryKeyName] as Dictionary<string, string>;
				if (c != null) return c;

				c = new Dictionary<string, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(DicNameByIdDictionaryKeyName, c, GetTableCacheDependency(DicNameByIdDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] DicValueByIdDictionaryDependencyTables = GetDependencyTables("DicValueByIdDictionaryTableDependency", "Dictionary");
		private const string DicValueByIdDictionaryKeyName = "DicValueByIdDictionary";

		public Dictionary<string, string> DicValueByIdDictionary
		{
			get
			{
				Dictionary<string, string> c = cache[DicValueByIdDictionaryKeyName] as Dictionary<string, string>;
				if (c != null) return c;

				c = new Dictionary<string, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(DicValueByIdDictionaryKeyName, c, GetTableCacheDependency(DicValueByIdDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] DicClassIdByCodeDictionaryDependencyTables = GetDependencyTables("DicClassIdByCodeDictionaryTableDependency", "DicClass");
		private const string DicClassIdByCodeDictionaryKeyName = "DicClassIdByCodeDictionary";

		public Dictionary<string, int?> DicClassIdByCodeDictionary
		{
			get
			{
				Dictionary<string, int?> c = cache[DicClassIdByCodeDictionaryKeyName] as Dictionary<string, int?>;
				if (c != null) return c;

				c = new Dictionary<string, int?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(DicClassIdByCodeDictionaryKeyName, c, GetTableCacheDependency(DicClassIdByCodeDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] ModuleNameByIdDictionaryDependencyTables = GetDependencyTables("ModuleNameByIdDictionaryTableDependency", "Module");
		private const string ModuleNameByIdDictionaryKeyName = "ModuleNameByIdDictionary";

		public Dictionary<int, string> ModuleNameByIdDictionary
		{
			get
			{
				Dictionary<int, string> c = cache[ModuleNameByIdDictionaryKeyName] as Dictionary<int, string>;
				if (c != null) return c;

				c = new Dictionary<int, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(ModuleNameByIdDictionaryKeyName, c, GetTableCacheDependency(ModuleNameByIdDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] ModuleIdByCodeDictionaryDependencyTables = GetDependencyTables("ModuleIdByCodeDictionaryTableDependency", "Module");
		private const string ModuleIdByCodeDictionaryKeyName = "ModuleIdByCodeDictionary";

		public Dictionary<string, int?> ModuleIdByCodeDictionary
		{
			get
			{
				Dictionary<string, int?> c = cache[ModuleIdByCodeDictionaryKeyName] as Dictionary<string, int?>;
				if (c != null) return c;

				c = new Dictionary<string, int?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(ModuleIdByCodeDictionaryKeyName, c, GetTableCacheDependency(ModuleIdByCodeDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] ModuleCodeByIdDictionaryDependencyTables = GetDependencyTables("ModuleCodeByIdDictionaryTableDependency", "Module");
		private const string ModuleCodeByIdDictionaryKeyName = "ModuleCodeByIdDictionary";

		public Dictionary<int, string> ModuleCodeByIdDictionary
		{
			get
			{
				Dictionary<int, string> c = cache[ModuleCodeByIdDictionaryKeyName] as Dictionary<int, string>;
				if (c != null) return c;

				c = new Dictionary<int, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(ModuleCodeByIdDictionaryKeyName, c, GetTableCacheDependency(ModuleCodeByIdDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] SiteIdByModuleIdDictionaryDependencyTables = GetDependencyTables("SiteIdByModuleIdDictionaryTableDependency", "Module");
		private const string SiteIdByModuleIdDictionaryKeyName = "SiteIdByModuleIdDictionary";

		public Dictionary<int, int?> SiteIdByModuleIdDictionary
		{
			get
			{
				Dictionary<int, int?> c = cache[SiteIdByModuleIdDictionaryKeyName] as Dictionary<int, int?>;
				if (c != null) return c;

				c = new Dictionary<int, int?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(SiteIdByModuleIdDictionaryKeyName, c, GetTableCacheDependency(SiteIdByModuleIdDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] DescendantIdsByParentIdDictionaryDependencyTables = GetDependencyTables("DescendantIdsByParentIdDictionaryTableDependency", "Module");
		private const string DescendantIdsByParentIdDictionaryKeyName = "DescendantIdsByParentIdDictionary";

		public Dictionary<int, int[]> DescendantIdsByParentIdDictionary
		{
			get
			{
				Dictionary<int, int[]> c = cache[DescendantIdsByParentIdDictionaryKeyName] as Dictionary<int, int[]>;
				if (c != null) return c;

				c = new Dictionary<int, int[]>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(DescendantIdsByParentIdDictionaryKeyName, c, GetTableCacheDependency(DescendantIdsByParentIdDictionaryDependencyTables));

				return c;
			}
		}



		private static readonly string[] PathCodesByIdDictionaryDependencyTables = GetDependencyTables("PathCodesByIdDictionaryTableDependency", "Module");
		private const string PathCodesByIdDictionaryKeyName = "PathCodesByIdDictionary";

		public Dictionary<int, string[]> PathCodesByIdDictionary
		{
			get
			{
				Dictionary<int, string[]> c = cache[PathCodesByIdDictionaryKeyName] as Dictionary<int, string[]>;
				if (c != null) return c;

				c = new Dictionary<int, string[]>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(PathCodesByIdDictionaryKeyName, c, GetTableCacheDependency(PathCodesByIdDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] ModuleTreeListByIdDictionaryDependencyTables = GetDependencyTables("ModuleTreeListByIdDictionaryTableDependency", "Module");
		private const string ModuleTreeListByIdDictionaryKeyName = "ModuleTreeListByIdDictionary";

		public Dictionary<string, List<Module>> ModuleTreeListByIdDictionary
		{
			get
			{
				Dictionary<string, List<Module>> c = cache[ModuleTreeListByIdDictionaryKeyName] as Dictionary<string, List<Module>>;
				if (c != null) return c;

				c = new Dictionary<string, List<Module>>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(ModuleTreeListByIdDictionaryKeyName, c, GetTableCacheDependency(ModuleTreeListByIdDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] LinkClassNameByIdDictionaryDependencyTables = GetDependencyTables("LinkClassNameByIdDictionaryTableDependency", "LinkClass");
		private const string LinkClassNameByIdDictionaryKeyName = "LinkClassNameByIdDictionary";

		public Dictionary<int, string> LinkClassNameByIdDictionary
		{
			get
			{
				Dictionary<int, string> c = cache[LinkClassNameByIdDictionaryKeyName] as Dictionary<int, string>;
				if (c != null) return c;

				c = new Dictionary<int, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(LinkClassNameByIdDictionaryKeyName, c, GetTableCacheDependency(LinkClassNameByIdDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] LinkClassIdByCodeDictionaryDependencyTables = GetDependencyTables("LinkClassIdByCodeDictionaryTableDependency", "LinkClass");
		private const string LinkClassIdByCodeDictionaryKeyName = "LinkClassIdByCodeDictionary";

		public Dictionary<string, int?> LinkClassIdByCodeDictionary
		{
			get
			{
				Dictionary<string, int?> c = cache[LinkClassIdByCodeDictionaryKeyName] as Dictionary<string, int?>;
				if (c != null) return c;

				c = new Dictionary<string, int?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(LinkClassIdByCodeDictionaryKeyName, c, GetTableCacheDependency(LinkClassIdByCodeDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] VoteClassIdByCodeDictionaryDependencyTables = GetDependencyTables("VoteClassIdByCodeDictionaryTableDependency", "VoteClass");
		private const string VoteClassIdByCodeDictionaryKeyName = "VoteClassIdByCodeDictionary";

		public Dictionary<string, int?> VoteClassIdByCodeDictionary
		{
			get
			{
				Dictionary<string, int?> c = cache[VoteClassIdByCodeDictionaryKeyName] as Dictionary<string, int?>;
				if (c != null) return c;

				c = new Dictionary<string, int?>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(VoteClassIdByCodeDictionaryKeyName, c, GetTableCacheDependency(VoteClassIdByCodeDictionaryDependencyTables));

				return c;
			}
		}




		private static readonly string[] VoteClassNameByIdDictionaryDependencyTables = GetDependencyTables("VoteClassNameByIdDictionaryTableDependency", "VoteClass");
		private const string VoteClassNameByIdDictionaryKeyName = "VoteClassNameByIdDictionary";

		public Dictionary<int, string> VoteClassNameByIdDictionary
		{
			get
			{
				Dictionary<int, string> c = cache[VoteClassNameByIdDictionaryKeyName] as Dictionary<int, string>;
				if (c != null) return c;

				c = new Dictionary<int, string>();

				AggregateCacheDependency dependency = new AggregateCacheDependency();
				cache.Insert(VoteClassNameByIdDictionaryKeyName, c, GetTableCacheDependency(VoteClassNameByIdDictionaryDependencyTables));

				return c;
			}
		}
		#endregion
	}
}